part of '../component.dart';

typedef BoolResultCallback = bool Function();

class CodeField extends StatefulWidget {
  const CodeField({
    Key? key,
    required this.controller,
    required this.smsClickListener,
    this.isLineStyle = true,
    this.fillColor,
    this.maxLength = 6,
    this.errText,
    this.onChange,
    this.timeCount = 60,
    this.fontSize,
    this.sendAction,
    this.height,
    this.borderRadius,
    this.margin,
  }) : super(key: key);
  final TextEditingController controller;
  final Function() smsClickListener;
  final bool isLineStyle;
  final Color? fillColor;
  final int maxLength;
  final String? errText;
  final Function(String)? onChange;
  final int timeCount;
  final double? fontSize;
  final BoolResultCallback? sendAction;
  final double? height;
  final double? borderRadius;
  final double? margin;
  @override
  _CodeFieldState createState() => _CodeFieldState();
}

class _CodeFieldState extends State<CodeField> {
  String actionText = '获取验证码';
  int timeCount = 60;
  Timer? timer;
  bool isSending = false;

  @override
  void initState() {
    super.initState();
    timeCount = widget.timeCount;
  }

  @override
  void dispose() {
    super.dispose();
    timer?.cancel();
  }

  void sendAction() async {
    if (widget.sendAction != null) {
      if (!widget.sendAction!()) {
        return;
      }
    }
    if (isSending) {
      return;
    }

    bool b = await widget.smsClickListener();

    if (!b) {
      return;
    }
    isSending = true;
    timer = Timer.periodic(const Duration(seconds: 1), (_) {
      if (timeCount == 0) {
        isSending = false;
        timer?.cancel();
        timeCount = widget.timeCount;
        actionText = '获取验证码';
      } else {
        timeCount--;
        actionText = '${timeCount}s';
      }
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: widget.height ?? 50.r,
      alignment: Alignment.center,
      child: TextField(
        controller: widget.controller,
        maxLength: widget.maxLength,
        keyboardType: TextInputType.number,
        textAlignVertical: TextAlignVertical.center,
        onTap: () {
          if (widget.onChange != null) {
            widget.onChange!('');
          }
        },
        onChanged: (value) {
          if (widget.onChange != null) {
            widget.onChange!(value);
          }
        },
        style: TextStyle(
          color: const Color(0xff333333),
          fontSize: widget.fontSize ?? 15.sp,
        ),
        decoration: InputDecoration(
          isCollapsed: true,
          counterText: '',
          errorText: widget.errText,
          filled: !widget.isLineStyle,
          fillColor: widget.fillColor ?? const Color(0xfffafafa),
          suffixIcon: InkWell(
              onTap: sendAction,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    width: 70.r,
                    height: (widget.height ?? 50.r) - 14.r,
                    margin: EdgeInsets.only(right: widget.margin ?? 10.r),
                    padding: widget.isLineStyle
                        ? EdgeInsets.zero
                        : const EdgeInsets.fromLTRB(0, 5, 0, 5),
                    decoration: widget.isLineStyle
                        ? null
                        : BoxDecoration(
                            color: isSending
                                ? const Color(0xffF4F6FA)
                                : ITheme.selectedColor,
                            borderRadius: BorderRadius.circular(5),
                          ),
                    alignment: widget.isLineStyle
                        ? Alignment.centerRight
                        : Alignment.center,
                    child: Text(
                      actionText,
                      style: TextStyle(
                          color: isSending
                              ? const Color(0xff999999)
                              : (widget.isLineStyle
                                  ? ITheme.selectedColor
                                  : Colors.white),
                          fontSize: 12.sp),
                    ),
                  ),
                ],
              )),
          hintText: '请输入验证码',
          hintStyle: TextStyle(
              color: const Color(0xffdddddd),
              fontSize: widget.fontSize ?? 15.sp),
          contentPadding: widget.isLineStyle
              ? EdgeInsets.zero
              : EdgeInsets.only(left: widget.margin ?? 10.r),
          border: InputBorder.none,
          errorBorder: widget.isLineStyle
              ? const UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.red),
                  borderRadius: BorderRadius.zero)
              : OutlineInputBorder(
                  borderRadius:
                      BorderRadius.circular(widget.borderRadius ?? 5.r),
                  borderSide: const BorderSide(color: Colors.red, width: 0.5),
                ),
          enabledBorder: widget.isLineStyle
              ? const UnderlineInputBorder(
                  borderSide: BorderSide(color: Color(0xffdddddd)),
                  borderRadius: BorderRadius.zero)
              : OutlineInputBorder(
                  borderRadius:
                      BorderRadius.circular(widget.borderRadius ?? 5.r),
                  borderSide:
                      const BorderSide(color: Color(0xffdddddd), width: 0.5),
                ),
          focusedBorder: widget.isLineStyle
              ? const UnderlineInputBorder(
                  borderSide: BorderSide(color: Color(0xffdddddd)),
                  borderRadius: BorderRadius.zero)
              : OutlineInputBorder(
                  borderRadius:
                      BorderRadius.circular(widget.borderRadius ?? 5.r),
                  borderSide:
                      BorderSide(color: ITheme.selectedColor, width: 0.5),
                ),
        ),
      ),
    );
  }
}
